package com.superman.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class JDBCTemplate {

    /**
     * 增删改模板类方法
     * @return 受影响的行数
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public static int update(String sql, Object[] params) throws SQLException, ClassNotFoundException {
        Connection conn = ConnectionFactory.getConnection();
        PreparedStatement pstmt = conn.prepareStatement(sql);

        if(params != null){
            for(int i=0; i<params.length; i++){
                pstmt.setObject(i+1,params[i]);
            }
        }
        int rows = pstmt.executeUpdate();
        ConnectionFactory.close(pstmt,conn);
        return rows;
    }

    /**
     * 增删改模板类方法，事务中保证单一连接
     * @param sql 要执行的sql语句
     * @param params 占位符参数数组
     * @param conn 事务中的单一连接
     * @return 受影响的行数
     * @throws SQLException
     */
    public static int update(String sql, Object[] params, Connection conn) throws SQLException {
        PreparedStatement pstmt = conn.prepareStatement(sql);

        if(params != null){
            for(int i=0; i<params.length; i++){
                pstmt.setObject(i+1, params[i]);
            }
        }
        int rows = pstmt.executeUpdate();
        ConnectionFactory.close(pstmt);
        return rows;
    }

    /**
     *多行查询
     * @param sql 要执行的sql语句
     * @param params 占位符参数数组
     * @param handler 处理一行结果集的代码对象
     * @param <T> 泛型 结果集中一行对应的对象类型
     * @return 对象集合
     * @throws SQLException
     */
    public static <T> List<T> query(String sql, Object[] params, ResultSetHandler<T> handler) throws SQLException, ClassNotFoundException {
        Connection conn = ConnectionFactory.getConnection();
        PreparedStatement pstmt = conn.prepareStatement(sql);

        if(params != null){
            for(int i=0; i<params.length; i++){
                pstmt.setObject(i+1, params[i]);
            }
        }

        ResultSet rs = pstmt.executeQuery();
        List<T> list = new ArrayList<>();
        if(handler != null){
            while(rs.next()){
                T obj = handler.handleRow(rs);
                list.add(obj);
            }
        }
        ConnectionFactory.close(rs,pstmt,conn);
        return list;
    }

    /**
     * 单行查询
     * @param sql 要执行的sql语句
     * @param params 占位符参数数组
     * @param handler 处理一行结果集的代码对象
     * @param <T> 结果集中一行对应的对象类型
     * @return 一个对象
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public static <T> T singleQuery(String sql, Object[] params, ResultSetHandler<T> handler) throws SQLException, ClassNotFoundException {
        Connection conn = ConnectionFactory.getConnection();
        PreparedStatement pstmt = conn.prepareStatement(sql);

        if(params != null){
            for(int i=0; i<params.length; i++){
                pstmt.setObject(i+1,params[i]);
            }
        }
        ResultSet rs = pstmt.executeQuery();
        T obj = null;
        if(handler != null){
            if(rs.next()){
                obj = handler.handleRow(rs);
            }
        }
        ConnectionFactory.close(rs, pstmt, conn);
        return obj;
    }

}
